package cn.edu.zjgsu.ito.controller;
import cn.edu.zjgsu.ito.interceptor.AdminControllerInterceptor;
import cn.edu.zjgsu.ito.service.AdminExportPaymentService;
import cn.edu.zjgsu.ito.service.ServiceFactory;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;



import com.jfinal.aop.Before;
import com.jfinal.core.Controller;
import com.jfinal.kit.PathKit;
import com.jfinal.plugin.activerecord.Record;

@Before(AdminControllerInterceptor.class)
public class AdminExportPaymentController extends Controller{
	private AdminExportPaymentService adminExportPaymentService =ServiceFactory.createAdminExportPaymentService();
	private List<Record> records;
	private Record record;
	public static final DateFormat formatter=new SimpleDateFormat("yyyy-MM-dd-HH：mm");
	
	/**
	  *  导出Excel
	 * @throws FileNotFoundException 
	  * 
	  */
 	public void exportExcel() throws FileNotFoundException
 	{
 		Date d=new java.util.Date();
 		
        System.out.println("----------------------------------???????????????????????????????????????");
        String filedir=PathKit.getWebRootPath();
 		filedir=filedir+"\\WEB-INF\\download\\";
 		
        deleteFile(new File(filedir));//清空缓存区
         
 		String payMethod; 		
 		records=adminExportPaymentService.payment();
 		System.out.println(records);
 		payMethod=records.get(0).get("payMethod");
 		System.out.println(payMethod);
 		payMethod=payMethod.replace(" ", "-");
 		payMethod=payMethod+"缴费汇总表("+formatter.format(d)+").xls";
 		
 		OutputStream out = new FileOutputStream(filedir+payMethod);	 		
 		System.out.println(filedir+payMethod);
 		adminExportPaymentService.exportExcel(records, out);
 		String outpath=filedir+payMethod;
 		
 		renderFile(new File(outpath));
 	}
 	
 	 public void deleteFile(File oldPath) {
          if (oldPath.isDirectory()) {
           File[] files = oldPath.listFiles();
           for (File file : files) {
             deleteFile(file);
           }
          }else{
            oldPath.delete();
          }
        }

}
